package sourceagile.server.subversionFileRepository;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.tmatesoft.svn.core.SVNDirEntry;

import sourceagile.server.utilities.FileNameGenerator;

import com.github.api.v2.schema.Tree;

public class ConvertFileToJson {

	public static String FILE_PATH = "filePath";

	public static String FILE_NAME = "fileName";

	public static String DATE_MODIFIED = "dateModified";

	public static String FILE_AUTHOR = "fileAuthor";

	public static JSONObject convertFromSubversion(SVNDirEntry subversionEntry,
			String folder) {

		JSONObject entry = new JSONObject();

		try {

			entry.put(FILE_PATH, folder);

			entry.put(FILE_NAME, subversionEntry.getName());

			entry.put(DATE_MODIFIED, subversionEntry.getDate().getTime());

			entry.put(FILE_AUTHOR, subversionEntry.getAuthor());

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return entry;
	}

	public static JSONObject convertFromGithub(Tree tree) {

		JSONObject entry = new JSONObject();

		String treeName = tree.getName();

		String fileName = FileNameGenerator.getFileName(treeName);

		String filePath = FileNameGenerator.getFilePath(treeName, fileName);

		try {

			entry.put(FILE_PATH, filePath);

			entry.put(FILE_NAME, fileName);

			entry.put("type", tree.getType());

			entry.put("sha", tree.getSha());

			// entry.put(DATE_MODIFIED, githubEntry.getTime());

			// entry.put(FILE_AUTHOR, subversionEntry.getAuthor());

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return entry;
	}

}
